package com.example.fence.controller;

import com.example.fence.entity.Alarm;
import com.example.fence.entity.DeleteDevice;
import com.example.fence.entity.Fence;
import com.example.fence.enums.AlarmMode;
import com.example.fence.service.impl.AlarmServiceImpl;
import com.example.fence.util.FenceStoreUtil;
import com.example.fence.util.Result;
import com.example.fence.vo.AlarmVo;
import com.example.fence.vo.DeleteVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.delete.Delete;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.example.fence.util.ResultCode.FAIL;
import static com.example.fence.util.ResultCode.SUCCESS;

@RestController
@RequestMapping("alarm")
@Slf4j
@CrossOrigin
public class AlarmController {
    @Autowired
    private AlarmServiceImpl alarmServiceImpl;

    @PostMapping("createAlarmMode")
    public Result createAlarmType(@RequestBody AlarmVo alarmVo) throws MqttException {
        // 检查参数正确性
        // 整体上传了null
        if(alarmVo==null){
            log.warn("alarmVo is null!");
            return new Result(FAIL.code, false, null,"Alarm info is not existed!");
        }
        // 设备列表为空
        if(alarmVo.getDevicesName()==null||alarmVo.getDevicesName().isEmpty()){
            log.warn("Devices is null!");
            return new Result(FAIL.code, false, null,"Devices info is not existed!");
        }
        // 报警方式为空
        if(alarmVo.getAlarmMode()==null || alarmVo.getAlarmMode().isEmpty()){
            log.warn("AlarmMode is null!");
            return new Result(FAIL.code, false, null,"AlarmMode info is not existed!");
        }
        if(alarmVo.getSendInterval()<10 || alarmVo.getSendInterval()>86400){
            log.warn("SendInterval is illegal !");
            return new Result(FAIL.code, false, null,"SendInterval info is illegal!(10,86400)");
        }
        // 若上传的报警方式存在TEST则改为只有TEST
        for (String mode : alarmVo.getAlarmMode()) {  // 遍历上传的报警类型判断是否含有test
            if (mode.equalsIgnoreCase(AlarmMode.TEST.name())) {  // 含有test则改为只含test
                alarmVo.getAlarmMode().clear();
                alarmVo.getAlarmMode().add(AlarmMode.TEST.name());
                break;
            }
        }

        // 上传了错误的报警类型
        Result alarmModeCheck=alarmVo.validateAlarmModes();
        if(!alarmModeCheck.getSuccess()){
            return alarmModeCheck;
        }

        // 创建Alarm对象
        Alarm alarm=new Alarm(alarmVo);
        // 调用服务返回设置结果
        return alarmServiceImpl.createAlarmType(alarm);
    }
    @PostMapping("deleteAlarmMode")
    public Result deleteAlarmType(@RequestBody DeleteVo deleteVo) throws MqttException {
        // 检查参数正确性
        // 整体上传了null
        if(deleteVo==null){
            log.warn("deleteVo is null!");
            return new Result(FAIL.code, false, null,"delete info is not existed!");
        }
        // 设备列表为空
        if(deleteVo.getDevicesName()==null||deleteVo.getDevicesName().isEmpty()){
            log.warn("Devices is null!");
            return new Result(FAIL.code, false, null,"Devices info is not existed!");
        }

        // 上传了错误的报警类型
        Result deleteAlarm=deleteVo.validateDeleteAlarm();
        if(!deleteAlarm.getSuccess()){
            return deleteAlarm;
        }

        // 创建Alarm对象
        DeleteDevice deleteDevice=new DeleteDevice(deleteVo);
        // 调用服务返回设置结果
        return alarmServiceImpl.deleteDevice(deleteDevice);

    }

    /**
     * 获取报警信息
     * @param imei 设备imei号
     * @return 设备的报警信息
     */
    @GetMapping("getAlarm")
    public Result getAlarm(String imei){
        if(imei==null||imei.isEmpty()){
            log.warn("Imei is null!");
            return new Result(FAIL.code, false, null,"Device imei is not existed!");
        }
        return alarmServiceImpl.getAlarm(imei);
    }



    @GetMapping("getAlarmMode")
    public Map<String, Set<Integer>> getAlarmInfo(){
        return FenceStoreUtil.deviceAlarmMode;
    }
    @GetMapping("getSendEmail")
    public Map<String, String> getSendEmail(){
        return FenceStoreUtil.deviceSendEmail;
    }
    @GetMapping("getReceiveEmail")
    public Map<String, Set<String>> getReceiveEmail(){
        return FenceStoreUtil.deviceReceiveEmail;
    }


}
